{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "from lxml import etree\n",
    "html_str = \"\"\"\n",
    "<body>\n",
    "<div class=\"container\">\n",
    "    <div id=\"first\">\n",
    "        <div class=\"one\">都市</div>\n",
    "        <div class=\"two\">德玛西亚</div>\n",
    "        <div class=\"two\">王牌对王牌</div>\n",
    "        <a>\n",
    "            <div class=\"spe\">特殊位置</div>\n",
    "        </a>\n",
    "    </div>\n",
    "    <div id=\"second\">\n",
    "        <div class=\"three\">水电费</div>\n",
    "        <div class=\"three\">说的话房间不开封</div>\n",
    "        <div class=\"four\">三顿饭黑客技术</div>\n",
    "    </div>\n",
    "    <div id=\"third\">\n",
    "        <div class=\"three\">水电费</div>\n",
    "        <div class=\"three\">说的话房间开封</div>\n",
    "    </div>\n",
    "</div>\n",
    "</body>\n",
    "\"\"\"\n",
    "\n",
    "html = etree.HTML(html_str)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[<Element div at 0x7f8fefd4b960>]\n"
     ]
    }
   ],
   "source": [
    "print(html.xpath('.//div[@id=\"first\"]'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[<Element div at 0x7f8fefd7a5f0>, <Element div at 0x7f8fefd7a5a0>, <Element div at 0x7f8fefd7a910>, <Element div at 0x7f8fefd7ab40>, <Element div at 0x7f8fefd7ab90>, <Element div at 0x7f8fefd7abe0>, <Element div at 0x7f8fefd7ac30>, <Element div at 0x7f8fefd7ac80>, <Element div at 0x7f8fefd7acd0>, <Element div at 0x7f8fefd7ad20>, <Element div at 0x7f8fefd7ad70>, <Element div at 0x7f8fefd7adc0>, <Element div at 0x7f8fefd7ae10>]\n"
     ]
    }
   ],
   "source": [
    "print(html.xpath('.//div'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[<Element div at 0x7f8fefd7ad70>, <Element div at 0x7f8fefd7af50>, <Element div at 0x7f8fefd7afa0>, <Element div at 0x7f8fefd8c0a0>]\n"
     ]
    }
   ],
   "source": [
    "print(html.xpath('.//div[@class=\"three\"]'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['水电费', '说的话房间不开封', '水电费', '说的话房间开封']\n"
     ]
    }
   ],
   "source": [
    "print(html.xpath('.//div[@class=\"three\"]/text()'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[<Element div at 0x7f8fefd7a4b0>, <Element div at 0x7f8fefb04a00>]\n"
     ]
    }
   ],
   "source": [
    "print(html.xpath('.//div[@id=\"first\"]/div[@class=\"two\"]'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[<Element div at 0x7f8fefd4baa0>]\n"
     ]
    }
   ],
   "source": [
    "print(html.xpath('.//div[@id=\"first\"]/div[@class=\"one\"]'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
